import {BASE_WP_URL} from '../wp-url'
import axios from 'axios';

export const GET_POSTS = 'GET_POSTS';
export const GET_CATEGORIES = 'GET_CATEGORIES';
const WP = `${BASE_WP_URL}/wp/v2`

let defaultQuery = {
  page: 1,
  per_page: 3
}
export const getPosts = (query = defaultQuery) => {
  let catString = "";
  if (query.category) {
    catString = `filter[category_name]=${query.category}`;
    delete query.category;
  }
  let postQuery = {...defaultQuery,...query}
  let request = axios.get(`${WP}/posts?${catString}`,{
    params: postQuery
  });
  return {
    type: GET_POSTS,
    payload: request
  }

}

export const getCategories = () => {
  let request = axios.get(`${WP}/categories`);
  return {
    type: GET_CATEGORIES,
    payload: request
  }

}
